<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  admin_activity.php
//  www.6block.com
//  
//  Created by Will_Lee on 2011-05-19.
//  Copyright 2011 lishuzu. All rights reserved.
// 

class Admin_brand extends CI_Controller {

	 function __construct()
	 {
		parent::__construct();
 		$this->load->model('common/security_model');
		$this->load->model('brand_model');
		$this->category = $this->config->item('category');
		//加载缓存类
		$this->load->library('datacache');
		if($this->security_model->isAdminLogin() === FALSE){
          showmessage('请登录', 'admin/login/index', 3);
		}
		// 加载字符串函数
		$this->load->helper('string');
	 }
	
	function index(){
		$data = $this->brand_model->get_brands();
		//分类信息
		$data['category'] = $this->category;
		$this->load->template('admin/brand_tpl', $data);
	}
	
	function add_brand(){
		$set = $this->input->post('set');
		$thevaluesubmit = $this->input->post('thevaluesubmit') ? $this->input->post('thevaluesubmit') : '';
		if ($thevaluesubmit) {
		//验证表单
		$insertdata = array(
				'name' => trim($set['name']),
				'catid' => intval($set['catid']),
				'company' => $set['company'],
				'firstletter' => strtoupper(getfirstletter(trim($set['name']))),
				'note' => $set['note'],
				'dateline' => now(),
			);
		$bid = $this->brand_model->insert_brand($insertdata);
		if ($bid) {
			if ($_FILES['logo']['name']) {
				$upload = $this->_upload_logo($bid);
				if ($upload['error']) {
					showmessage('上传失败','admin/admin_brand/index', 3);				
				}else{
					$updatedata = array(
							'bid' => $bid,
							'logo' => $upload['upload_data']['file_name'],
						);
					$result = $this->brand_model->update_brand($updatedata);
				}
			}
			//更新品牌缓存
			$this->datacache->brand_cache();
			
			showmessage('添加成功','admin/admin_brand/index', 3);
		}else{
			showmessage('添加失败','admin/admin_brand/index', 3);				
		}

		}
		//分类信息
		$data['category'] = $this->category;
		$this->load->template('admin/add_brand_tpl', $data);
	}
	
	function edit_brand($bid){
		$set = $this->input->post('set');
		$thevaluesubmit = $this->input->post('thevaluesubmit') ? $this->input->post('thevaluesubmit') : '';
		$bid = $bid ? intval($bid) : 0;
		if (empty($bid)) {
			showmessage('非法来源');
		}
		$brand = $this->brand_model->get_brand_by_bid($bid);
		//表单提交
		if (!empty($thevaluesubmit) && !empty($bid)) {
			//验证表单
			
			$updatedata = array(
					'bid' => $bid,
					'name' => trim($set['name']),
					'catid' => intval($set['catid']),
					'company' => $set['company'],
					'note' => $set['note'],
				);
			if ($set['firstletter']) {
				$updatedata['firstletter'] = strtoupper(trim($set['firstletter']));
			}else{
				$updatedata['firstletter'] = strtoupper(getfirstletter(trim($set['firstletter'])));
			}
			if ($_FILES['logo']['name']) {
				$upload = $this->_upload_logo($bid);
				if ($upload['error']) {
					showmessage('上传失败','admin/admin_brand/index', 3);				
				}else{
					$updatedata['logo'] = $upload['upload_data']['file_name'];
				}
			}
			$result = $this->brand_model->update_brand($updatedata);
			if ($result) {
				//更新品牌缓存
				$this->datacache->brand_cache();
				showmessage('编辑成功','admin/admin_brand/index', 3);
			}else{
				showmessage('编辑失败','admin/admin_brand/index', 3);				
			}
			
		}
		
		$data['thevalue'] = $brand;
		//分类信息
		$data['category'] = $this->category;
		$this->load->template('admin/edit_brand_tpl', $data);	
	}
	
	//上传logo
	function _upload_logo($bid){
		if (empty($bid)) {
			return false;
		}
		$config['upload_path'] = FCPATH.'upload/logo/';
		$config['allowed_types'] = 'gif|jpg|png|jpeg';
		$config['max_size'] = '100';
		$config['max_width']  = '1024';
		$config['max_height']  = '768';
		$config['overwrite'] = TRUE;
		$config['file_name'] = $bid;
		$this->load->library('upload', $config);
		if (!$this->upload->do_upload('logo')){
			$error = array('error' => $this->upload->display_errors());
			return $error;
		}else{
			$data = array('upload_data' => $this->upload->data());
			return $data;
		}
	}
	//单个删除 controller
    function delete($bid)
    {
		if (empty($bid)) {
			showmessage('没有选择任何品牌');
		}
		
		$ids = array($bid);
		if (empty($ids)) {
			showmessage('没有选择任何品牌');
		}
		$result = $this->brand_model->deletes_brand($ids);
		if ($result) {
			//更新品牌缓存
			$this->datacache->brand_cache();
			showmessage('删除成功','admin/admin_brand/index', 3);	
		}else{
			showmessage('删除失败','admin/admin_brand/index', 3);
		}
    }

	//批量删除 controller
	function deletes()
	{	
		$ids = $this->input->post('ids');
		if (empty($ids)) {
			showmessage('没有选择任何品牌');
		}
		$result = $this->brand_model->deletes_brand($ids);
		if ($result) {
			//更新品牌缓存
			$this->datacache->brand_cache();
			showmessage('删除成功','admin/admin_brand/index', 3);	
		}else{
			showmessage('删除失败','admin/admin_brand/index', 3);
		}
	}
}